from captcha.image import ImageCaptcha
from flask import session
import random
import string
import base64
from io import BytesIO


def create_image_verify_code():
    list_letters = string.ascii_letters
    list_digits = string.digits
    list_code = list_digits + list_letters
    random_list = random.choices(list_code, k=4)
    chars = ''.join(random_list)
    image = ImageCaptcha().generate_image(chars)
    img_buffer = BytesIO()
    image.save(img_buffer, format('JPEG'))
    byte_data = img_buffer.getvalue()
    base64_str = base64.b64encode(byte_data)
    print(base64_str)
    session['image_verify_code'] = chars
    print(chars)
    base64_str = 'data:image/jpeg;base64,%s' % base64_str
    return base64_str, 200, {"Content-Type": "image/jpeg"}


